/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lyh.api.utils;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.nio.charset.Charset;

/**
 * @author shoushen.luan
 * @date 2022/2/25 9:55 上午
 */
public class TestDump {
    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

    /**
     * Dump byte array into a hex string. See
     * https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java
     *
     * @param bytes bytes
     * @return hex string
     */
    public static String toHex(final byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        final char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }

    /**
     * Convert a string representation of a hex dump to a byte array. See
     * https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
     *
     * @param s hex string
     * @return bytes
     */
    public static byte[] hexStringToByteArray(final String s) {
        if (s == null) {
            return null;
        }
        final int len = s.length();
        final byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            bytes[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
        }
        return bytes;
    }

    public static void main(String[] args) {
        AbstractRejectedExecutionHandler executionHandler = new AbstractRejectedExecutionHandler("myThread", true,
            "dumpJVM") {
            @Override
            public void dumpJvmInfoIfNeeded() {
                super.dumpJvmInfoIfNeeded();
            }
        };
        //    executionHandler.dumpJvmInfoIfNeeded();
        String str = "测试消息！@￥3r523sdf 水电🇨🇳2😁🏠3 ";
        String hex = toHex(str.getBytes(Charset.forName("utf-8")));
        System.out.println(hex);
        byte[] bytes = hexStringToByteArray(hex);
        System.out.println(new String(bytes));
        dumpThreads();
    }

    public static void dumpThreads() {
        System.out.println("------------------------dump-all-thread-stack----------------------------");
        try {
            ThreadMXBean bean = ManagementFactory.getThreadMXBean();
            ThreadInfo[] infos = bean.dumpAllThreads(true, true);
            for (ThreadInfo info : infos) {
                System.out.println(info);
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
